package org.util;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author huangshen
 * @version 0.0.3
 * @description StringUtils
 * @since 2022/1/14 10:00
 */
public class StringUtils {
    private static final Pattern CAMEL_PATTERN = Pattern.compile("([A-Za-z\\d]+)(_)?");
    private static final Pattern HUMP_PATTERN = Pattern.compile("[A-Z]");

    /**
     * 下划线转驼峰法
     *
     * @param line       源字符串
     * @param smallCamel 大小驼峰,是否为小驼峰
     * @return 转换后的字符串
     */
    public static String underline2Camel(String line, boolean smallCamel) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Matcher matcher = CAMEL_PATTERN.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0)) : Character.toUpperCase(word.charAt(0)));
            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰 转下划线
     *
     * @param camelCase 字符串
     * @return String
     */
    public static String camel2UnderLine(String camelCase) {
        Matcher matcher = HUMP_PATTERN.matcher(camelCase);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


    /**
     * 获取剔除变量名中下划线后的set、get方法
     *
     * @param name 变量名
     * @return String
     * @author huangshen
     * @since 2021/12/19 10:44
     */
    public static String toFirstLetterUpperCase(String name) {
        if (name == null || name.length() < 2) {
            return name;
        }
        String[] tmp = name.split("_");
        StringBuilder buf = new StringBuilder();
        buf.append(tmp[0]);
        for (int j = 1; j < tmp.length; j++) {
            buf.append(tmp[j].substring(0, 1).toUpperCase());
            buf.append(tmp[j].substring(1, tmp[j].length()));
        }
        String changeStr = buf.toString();
        String firstLetter = changeStr.substring(0, 1).toUpperCase();
        return firstLetter + changeStr.substring(1, changeStr.length());
    }

    /**
     * 解析Json封装进Map
     *
     * @param jsonStr json字符串
     * @return Map<String, String>
     * @author huangshen
     * @since 2021/12/19 10:44
     */
    public static Map<String, Object> parserJson2Map(String jsonStr) {
        if (jsonStr == null || jsonStr.trim().length() == 0) {
            return Collections.emptyMap();
        }
        Map<String, Object> res = new LinkedHashMap<>();
        // 过滤左右符号
        jsonStr = jsonStr.replace("{", "");
        jsonStr = jsonStr.replace("}", "");
        jsonStr = jsonStr.replace("\"", "");
        jsonStr = jsonStr.replace("'", "");
        String[] strM = jsonStr.split(",");
        for (String str : strM) {
            String[] tem = str.split(":");
            if (tem.length == 2) {
                res.put(tem[0], tem[1]);
            }
        }
        return res;
    }


    /**
     * 字符串数据脱敏
     *
     * @param s         待脱敏字符串
     * @param begin     起始保留位数
     * @param end       末尾保留位数
     * @param regex     需要替换为 * 的字符的正则表达式
     * @param isDecrypt 是否需要解密
     * @return
     */
    private static String removal(String s, int begin, int end, String regex, boolean isDecrypt) {
        String sub = s.substring(begin, s.length() - end);
        String temp = sub.replaceAll(regex, "*");
        char[] chars = s.toCharArray();
        for (int i = 0; i < temp.length(); i++) {
            chars[i + begin] = temp.charAt(i);
        }
        return String.copyValueOf(chars);
    }

    public static String removalName(String s) {
        int end = s.length() > 2 ? 1 : 0;
        return removal(s, 1, end, ".", false);
    }

    public static String removalNumber(String s, int begin, int end) {
        return removal(s, begin, end, "[0-9]", false);
    }

    /**
     * 根据文件命名规则生成文件名
     * 文件名中的xxx表示省编码；文件名中的_yyyyMMddHHmmss表示文件生成日期；文件后缀.txt表示文件为txt文本文件
     *
     * @param fileHead 文件名
     * @param suffix   文件后缀名
     * @return fileName
     * @author huangshen
     * @since 2021/12/19 10:44
     */
    public static String createFileName(String fileHead, String suffix) {
        StringBuilder ssTmpFileName = new StringBuilder();
        ssTmpFileName.append(fileHead).append("_");
        ssTmpFileName.append(DateTime.format("yyyyMMddHHmmss", DateTime.getDate()));
        ssTmpFileName.append(suffix);
        return ssTmpFileName.toString();
    }

}
